<template>
  <div id="login_page">
    <!-- 登陆区域 开始 -->
    <van-nav-bar title="登录" class="page-nav-login">
      <template #left>
        <van-icon @click="$router.go(-1)" name="cross" size="18"/>
      </template>
    </van-nav-bar>
    <!-- 登陆区域 结束 -->
    <van-form @submit="onSubmit" ref="loginForm">
      <van-field
        v-model="mobile"
        name="mobile"
        placeholder="手机号"
        :rules="loginFormRules.mobile"
        type="number"
        maxlength="11"
      >
        <i slot="left-icon" class="toutiao toutiao-shouji"></i>
      </van-field>
      <van-field
        v-model="code"
        name="code"
        placeholder="验证码"
        :rules="loginFormRules.code"
        type="number"
        maxlength="6"
      >
        <i slot="left-icon" class="toutiao toutiao-yanzhengma"></i>
        <template #button>
          <van-button
            v-if="sendCodeBtnVisible"
            class="second-code-btn"
            size="small"
            type="primary"
            native-type="button"
            @click="sendCode"
            >获取验证码</van-button
          >
          <van-count-down
            @finish="sendCodeBtnVisible = true"
            :time="60 * 1000"
            format="ss s"
            v-else
          />
        </template>
        <!-- <van-button v-slot:button class="second-code-btn" size="small" type="primary">获取验证码</van-button> -->
      </van-field>
      <div style="margin: 16px" class="submit-login">
        <van-button block type="info" native-type="submit" class="login-btn"
          >登录</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { login, sendMobileCode } from '@/api/user.js'
export default {
  data() {
    return {
      // 控制显示与否
      sendCodeBtnVisible: true,
      mobile: '13911111111',
      code: '246810',
      loginFormRules: {
        mobile: [
          { required: true, message: '请填写手机号' },
          {
            pattern: /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/,
            message: '请填写正确的手机号'
          }
        ],
        code: [
          { required: true, message: '请填写验证码' },
          { pattern: /^\d{6}$/, message: '请填写正确的验证码' }
        ]
      }
    }
  },

  created() {},

  methods: {
    // 提交表单
    async onSubmit() {
      this.$toast.loading({
        message: '加载中...',
        forbidClick: true,
        duration: 0
      })
      try {
        const res = await login({
          mobile: this.mobile,
          code: this.code
        })
        this.$toast.success('登录成功')
        this.$store.commit('setUser', res.data)
      } catch (error) {
        // 先验证err上是否存在
        if (error.response && error.response.status === 400) {
          return this.$toast.fail('手机号或验证码错误')
        }
        this.$toast.fail('系统异常')
      }
    },
    // 点击发送验证码
    async sendCode() {
      try {
        // 加await修饰是因为vant的validate的返回值是一个promise，会异步执行，即使发生错误也会先执行同步代码
        await this.$refs.loginForm.validate('mobile')
      } catch (error) {
        // 不要忘记加return不然会继续执行try catch以外的语句
        return console.log('手机号码错误')
      }
      // 发送请求成功前先来一个发送中的消息提示
      this.$toast.loading({
        message: '发送中...',
        forbidClick: true,
        duration: 0
      })
      try {
        await sendMobileCode({ mobile: this.mobile })
        // 等发送成功后再让倒计时显示
        this.sendCodeBtnVisible = false
        this.$toast.success('发送成功')
      } catch (error) {
        if (error.response && error.response.status === 429) {
          return this.$toast.fail('验证码发送过于频繁')
        }
        this.$toast('系统异常')
      }
    }
  }
}
</script>

<style scoped lang='less'>
.toutiao {
  font-size: 37px;
}
.second-code-btn {
  width: 152px;
  height: 46px;
  background-color: #ededed;
  border: none;
  border-radius: 23px;
  color: #666;
}
.submit-login {
  margin: 58px 23px;
  .login-btn {
    width: 694px;
    height: 88px;
    background: #6db4fb;
    border-radius: 10px;
    border: none;
  }
}
</style>
